#!/usr/bin/perl

use strict;
use XML::Simple;

my $cfgfile = shift;
my $chan = shift;

if (not defined $chan) {
	die "Usage: nagios-email config.xml #chan";
}

my $CONFIG = XMLin($cfgfile, ForceArray => ['automsg', 'autojoin']);

my $client = "/".$CONFIG->{'directory'}."/scripts/client";
my $socket = "/".$CONFIG->{'directory'}."/".$CONFIG->{'socket'};

my $data;
my $inhead = 1;
while (<>) {
	chomp;
	if (/^Subject:/ and $inhead == 1) {
		s/^Subject:\s*//;
		$data = $_;
	}
	elsif (/^$/ and $inhead == 1) {
		$inhead = 0;
	}
	elsif (/^Info/ and $inhead == 0) {
		s/^Info:\s*//;
		$data .= ", $_";
	}
	elsif (/^Date/ and $inhead == 0) {
		s/^Date:\s*//;
		$data .= " $_";
	}
}
chomp($data);

open(CLIENT, "| $client $socket");
print(CLIENT "to $chan\n");
print(CLIENT "notice $data\n");
close(CLIENT);
#print("to $chan\n");
#print("notice $data\n");
